<?php
/**
 * User: aka
 * Date: 2018/6/26
 * Time: 17:21
 */

namespace app\backstage\model;


use app\common\db\DbOperation;

class Food
{
    static private $classification = 'classification_info';
    static private $food = 'food_info';

    public $dbOperation;

    public function __construct()
    {
        $this->dbOperation = new DbOperation();
    }

    public function __destruct()
    {
        unset($this->dbOperation);
    }

    private function getDbOperation() : DbOperation
    {
        return $this->dbOperation;
    }

    public function classificationAddAction(array $requestData) : bool
    {
        return $this->getDbOperation()->add(self::$classification,['classification_name'=>$requestData['classification_name']]);
    }

    public function classificationEditAction(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$classification,$requestData['id'],['classification_name'=>$requestData['classification_name']]);
    }

    public function classificationSubAction(array $requestData) : bool
    {
        return $this->getDbOperation()->add(self::$classification,['classification_name'=>$requestData['classification_name'],'pid'=>$requestData['pid']]);
    }

    private function DisableOperation(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$classification,$requestData['id'],['status' => 0]);
    }

    private function EnableOperation(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$classification,$requestData['id'],['status' => 1]);
    }

    public function typeStatusOperationAction(array $requestData) : int
    {
        $findImgData = $this->getDbOperation()->find(self::$classification,['id'=>$requestData['id']]);
        if ($findImgData['status'] == 1)
        {
            $result = $this->DisableOperation($requestData);
            return !$result?:0;
        }else{
            $result = $this->EnableOperation($requestData);
            return !$result?:1;
        }
    }

    public function foodAddAction(array $requestData,string $singleImgUrl) : bool
    {
        $addData = [
            'food_name'=>$requestData['food_name'],
            'food_price'=>$requestData['food_price'],
            'food_des'=>$requestData['food_des'],
            'food_classification'=>$requestData['food_classification'],
            'food_img'=>$singleImgUrl,
            'time'=>time()
        ];
        return $this->getDbOperation()->add(self::$food,$addData);
    }

    public function foodStatusOperationAction(array $requestData) : int
    {
        $findImgData = $this->getDbOperation()->find(self::$food,['id'=>$requestData['id']]);
        if ($findImgData['status'] == 1)
        {
            $result = $this->foodStatusDisableOperation($requestData);
            return !$result?:0;
        }else{
            $result = $this->foodStatusEnableOperation($requestData);
            return !$result?:1;
        }
    }

    private function foodStatusDisableOperation(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$food,$requestData['id'],['status' => 0]);
    }

    private function foodStatusEnableOperation(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$food,$requestData['id'],['status' => 1]);
    }

    public function foodTopOperationAction(array $requestData) : int
    {
        $findImgData = $this->getDbOperation()->find(self::$food,['id'=>$requestData['id']]);
        $type = db('classification_info')->where(['id'=>$findImgData['food_classification']])->find();
        if ($findImgData['top'] == 0)
        {
            $result = $this->foodTopOpenOperation($requestData,$type);
            return !$result?:1;
        }else{
            $result = $this->foodTopCanelOperation($requestData);
            return !$result?:0;
        }
    }

    private function foodTopCanelOperation(array $requestData) : bool
    {
        return $this->getDbOperation()->update(self::$food,$requestData['id'],['top' => 0]);
    }

    private function foodTopOpenOperation(array $requestData,array $type) : bool
    {
        return $this->getDbOperation()->update(self::$food,$requestData['id'],['top' => $type['pid']]);
    }

    public function foodImgEditAction(string $id,string $singleImgUrl) : bool
    {
        return $this->getDbOperation()->update(self::$food,$id,['food_img'=>$singleImgUrl]);
    }

    public function foodInfoEditAction(array $requestData) : bool
    {
        $updateData = [
            'food_name'=>$requestData['food_name'],
            'food_price'=>$requestData['food_price'],
            'food_des'=>$requestData['food_des'],
            'food_classification'=>$requestData['food_classification']
        ];

        return $this->getDbOperation()->update(self::$food,$requestData['id'],$updateData);
    }
}